#!/bin/bash
# 安装nacos

# 官网 nacos.io
# 下载 github.com/alibaba/nacos/releases/

. ./public_functions &>/dev/null #公共函数库
. /tmp/public_functions &>/dev/null #公共函数库

##1 二进制安装nacos
nacos_Install_centos7() {

  readonly nacos_Package="nacos-server-2.1.1.tar.gz"	# nacos压缩包名称
  readonly nacos_Url_local=$(find / -path /proc -prune -o -name $nacos_Package | grep -Eo "/.*/")  # 获取本地软件包路径
  readonly nacos_Catalog="nacos"		        # 压缩包解压后的目录名称

  logW "1. 查找 $nacos_Package" #1-20#
  logY "find / -name $nacos_Package" #1-20#
    find / -path /proc -prune -o -name $nacos_Package | grep -v '/proc'
    find / -path /proc -prune -o -name $nacos_Package | grep -Eo --silent "$nacos_Package"
  checkTF #21#

  logW "2. 开始解压 $nacos_Package" #1-20#
  logY "tar -xzf ${nacos_Url_local}${nacos_Package} -C /usr/local/" #1-20#
    tar -xzf ${nacos_Url_local}${nacos_Package} -C /usr/local/
  checkTF #21#

#  logW "3. 修改配置文件 /usr/local/$nacos_Catalog/conf/application.properties" #1-20#
#
#    var1="spring.datasource.platform=mysql"
#    var2="db.num=1"
#    var3="db.url.0=jdbc:mysql://10.114.12.177:3306/nacos?serverTimezone=GMT%2B8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true"
#    var4="db.user.0=root"
#    var5="db.password.0=root"
#
#    for i in "$var1" "$var2" "$var3" "$var4" "$var5"
#    do
#      y=$( echo $i | cut -d= -f1 )
#      logY "修改 $y 的值"
#      grep "^$i" /usr/local/nacos/conf/application.properties || sed -i "/$y/a $i" /usr/local/nacos/conf/application.properties
#      checkTF #21#
#    done

#  logW "4. 创建用于nacos启动的systemd文件" #1-20#
#  logY "cat >/usr/lib/systemd/system/nacos.service <<-EOF..." #1-20#
#cat >/usr/lib/systemd/system/nacos.service <<-EOF
#[Unit]
#Description=nacos
#After=network.target
#[Service]
#Type=forking
#ExecStart=/usr/local/nacos/bin/startup.sh -m standalone
#ExecReload=/usr/local/nacos/bin/shutdown.sh
#ExecStop=/usr/local/nacos/bin/shutdown.sh
#PrivateTmp=true
#[Install]
#WantedBy=multi-user.target
#EOF
#  checkTF #21#
#  logY "systemctl daemon-reload" #1-20#
#    systemctl daemon-reload
#  checkTF #21#

  logW "5. 手动启动nacos服务" #1-20#
  logY "/usr/local/nacos/bin/startup.sh -m standalone" #1-20#
    source /etc/profile
    /usr/local/nacos/bin/startup.sh -m standalone
  checkTF #21#

  logW "6. 放行8848端口"
  get_Firewalld_status #23#
    if [[ $? -eq 0 ]]; then
      logY "firewall-cmd --zone=public --add-port="8848"/"tcp" --permanent" #1-20#
      firewall-cmd --zone=public --add-port="8848"/"tcp" --permanent 2>/dev/null
      firewall-cmd --reload &>/dev/null
      echo ""
    else
      logR "防火墙未开启" #1-20#
      echo ""
    fi

    echo "PS: nacos如有启动失败等... 可查看日志 /usr/local/nacos/logs/start.out" && echo

}

##1 二进制安装nacos
nacos_Install_ubuntu() {

  readonly nacos_Package="nacos-server-2.1.1.tar.gz"	# nacos压缩包名称
  readonly nacos_Url_local=$(sudo find / -path /proc -prune -o -name $nacos_Package | grep -Eo "/.*/")  # 获取本地软件包路径
  readonly nacos_Catalog="nacos"		        # 压缩包解压后的目录名称

  logW "1. 查找 $nacos_Package" #1-20#
  logY "sudo find / -name $nacos_Package" #1-20#
    sudo find / -path /proc -prune -o -name $nacos_Package | grep -v '/proc'
    sudo find / -path /proc -prune -o -name $nacos_Package | grep -Eo --silent "$nacos_Package"
  checkTF #21#

  logW "2. 开始解压 $nacos_Package" #1-20#
  logY "sudo tar -xzf ${nacos_Url_local}${nacos_Package} -C /usr/local/" #1-20#
    sudo tar -xzf ${nacos_Url_local}${nacos_Package} -C /usr/local/
  checkTF #21#

#  logW "3. 修改配置文件 /usr/local/$nacos_Catalog/conf/application.properties" #1-20#
#
#    var1="spring.datasource.platform=mysql"
#    var2="db.num=1"
#    var3="db.url.0=jdbc:mysql://10.114.12.177:3306/nacos?serverTimezone=GMT%2B8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true"
#    var4="db.user.0=root"
#    var5="db.password.0=root"
#
#    for i in "$var1" "$var2" "$var3" "$var4" "$var5"
#    do
#      y=$( echo $i | cut -d= -f1 )
#      logY "修改 $y 的值"
#      grep "^$i" /usr/local/nacos/conf/application.properties || sed -i "/$y/a $i" /usr/local/nacos/conf/application.properties
#      checkTF #21#
#    done

  logW "4. 手动启动nacos服务" #1-20#
  logY "" #1-20#
    source /etc/profile
    logR "请用root账户手动启动nacos，启动命令: /usr/local/nacos/bin/startup.sh -m standalone" && echo

  logW "5. 放行8848端口"
  get_Firewalld_status #23#
    if [[ $? -eq 0 ]]; then
      logY "firewall-cmd --zone=public --add-port="8848"/"tcp" --permanent" #1-20#
      firewall-cmd --zone=public --add-port="8848"/"tcp" --permanent 2>/dev/null
      firewall-cmd --reload &>/dev/null
      echo
    else
      logR "防火墙未开启" #1-20#
      echo
    fi

    echo "PS: nacos如有启动失败等... 可查看日志 /usr/local/nacos/logs/start.out" && echo

}

check_Version #25#

systemtype=$(uname -a | grep -Eo '(ubuntu|centos7)')

if [[ $systemtype == centos7 ]]; then 
   nacos_Install_centos7  ##1

elif [[ $systemtype == ubuntu ]]; then
     nacos_Install_ubuntu  ##2

fi

# sed -i 's/export JAVA=\"\$JAVA_HOME\/bin\/java\"/export JAVA_HOME=\/usr\/local\/jdk1.8.0_202/p' /usr/local/nacos/bin/startup.sh
